<template>
  <div class="app-container">
    <!-- 查询表单开始 -->
    <el-form :model="searchObj" inline="true">
      <!--横向表单-->
      <el-form-item>
        <!-- <el-input
          v-model="searchObj.name"
          placeholder="请输入讲师的姓名"
        ></el-input> -->
        <el-autocomplete
          value-key="name"
          class="inline-input"
          v-model="searchObj.name"
          :fetch-suggestions="querySearch"
          placeholder="请输入内容"
          :trigger-on-focus="false"
        ></el-autocomplete>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="searchObj.level"
          placeholder="头衔"
          clearable="true"
        >
          <el-option value="1" label="高级讲师"></el-option>
          <el-option value="2" label="首席讲师"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="入驻时间">
        <el-date-picker
          v-model="searchObj.joinDateBegin"
          placeholder="请选择"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="-">
        <el-date-picker
          v-model="searchObj.joinDateEnd"
          placeholder="请选择"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="fetchData()"
          >搜索</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 工具条 -->
    <div style="display: flex; justify-content: right;margin-bottom: 10px;">
      <el-button
        type="danger"
        @click="removeByIds"
        size="mini"
        icon="el-icon-delete"
        >批量删除</el-button
      >
    </div>
    <!-- 查询表单结束 -->
    <el-table
      :data="list"
      stripe
      border
      title="教师列表"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column width="50" label="序号">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="姓名" width="100"> </el-table-column>
      <el-table-column prop="level" label="头衔">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.level === 1">高级讲师</el-tag>
          <el-tag v-if="scope.row.level === 2">首席讲师</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="intro" label="资历"> </el-table-column>
      <el-table-column prop="sort" label="排序"> </el-table-column>
      <el-table-column prop="joinDate" label="入驻时间"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="mini"
            @click="removeById(scope.row.id)"
            icon="el-icon-remove"
            >删除</el-button
          >
          <router-link :to="'/teacher/edit/' + scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit"
              >修改</el-button
            >
          </router-link>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      layout="sizes,prev, pager, next, jumper, ->, total"
      :total="total"
      :page-sizes="[5, 10, 20]"
      :page-size="limit"
      :current-page="page"
      style="padding: 30px 0;"
    >
    </el-pagination>
  </div>
</template>

<script>
import teacher from "../../api/teacher";

export default {
  data() {
    return {
      multipleSelection: [],
      list: [], //讲师列表
      total: 0,
      page: 1,
      limit: 5,
      searchObj: {
        name: "",
        level: "",
        joinDateBegin: null,
        joinDateEnd: null,
      }, //查询条件
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      this.limit = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.fetchData();
    },
    //加载讲师列表
    fetchData() {
      teacher.pageList(this.page, this.limit, this.searchObj).then((resp) => {
        // console.log(resp.data.items);
        this.list = resp.data.rows;
        this.total = resp.data.total;
      });
    },
    /**
     * 根据id删除教师
     * @param {id} id
     */
    removeById(id) {
      this.$confirm("此操作将永久删除该教师, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          teacher.removeById(id).then((resp) => {
            if (resp) {
              this.fetchData();
              this.$message.success(resp.message);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    removeByIds() {
      if (this.multipleSelection.length === 0) {
        this.$message.error("请选择至少一项");
        return;
      }
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          const idList = [];
          this.multipleSelection.forEach((item) => {
            idList.push(item.id);
          });

          teacher.deleteByIds(idList).then((resp) => {
            if (resp) {
              this.fetchData();
              this.$message.success(resp.message);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    querySearch(queryString, callback) {
      teacher.selectNameListByKey(queryString).then((resp) => {
        if (resp) {
          //渲染下拉列表
          callback(resp.data.nameList);
        }
      });
    },
  },
};
</script>

<style scoped></style>>
